using System;
using System.Data;
using System.Data.SqlClient;

using PHSRAG.Insight21.BusinessObjects;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;

namespace PHSRAG.Insight21.ClinicalTrials
{
    /// <summary>
    /// The BudgetProcedure class is the facade over a single Budget Procedure
    /// </summary>
    public class BudgetProcedure : BusinessObject
    {

        #region Constants
        private const string BudgetProcedureKey = "BudgetProcedure_F52323F3-AE23-4135-BE10-4006E4B47755";
        private const string TableName = "BudgetProcedure";
        #endregion

        #region Instance Variables
        private DataRow dataRow;
        private BudgetMilestoneVisits budgetMilestoneVisits;
        #endregion

        #region Public Properties
        /// <summary>
        /// Unique Id for this Budget Procedure
        /// </summary>
        public int Id
        {
            get
            {
                object o = dataRow["Id"];
                return (o == DBNull.Value) ? 0 : (int)o;
            }
        }

        /// <summary>
        /// Budget relate id
        /// </summary>
        public int BudgetId
        {
            get
            {
                return (int)dataRow["ClinicalTrialBudgetId"];
            }
            set
            {
                dataRow["ClinicalTrialBudgetId"] = value;
            }
        }

        /// <summary>
        /// Procedure Charge Code (from charge master feed)
        /// </summary>
        public int ProcedureChargeCode
        {
            get
            {
                object o = dataRow["ProcedureChargeCode"];
                return (o == DBNull.Value) ? 0 : (int)o;
            }
            set
            {
                dataRow["ProcedureChargeCode"] = value;
            }
        }

        /// <summary>
        /// Procedure Cost
        /// </summary>
        public string ProcedureCost
        {
            get
            {
                return dataRow["ProcedureCost"].ToString();
            }
            set
            {
                dataRow["ProcedureCost"] = value;
            }
        }

        /// <summary>
        /// Procedure Name, used by the BudgetProcedureControl to display the corresponding name
        /// </summary>
        public string ProcedureName
        {
            get
            {
                return dataRow["ProcedureName"].ToString();
            }
            set
            {
                dataRow["ProcedureName"] = value;
            }
        }

        /// <summary>
        /// Boolean which indicates whether to apply overhead to this procedure
        /// </summary>
        public bool ApplyOverhead
        {
            get
            {
                object o = dataRow["ApplyOverhead"];
                return (o == DBNull.Value) ? false : (bool)o;
            }
            set
            {
                dataRow["ApplyOverhead"] = value;
            }
        }

        /// <summary>
        /// Boolean which indicates whether this is an inpatient procedure or not
        /// </summary>
        public bool Inpatient
        {
            get
            {
                object o = dataRow["Inpatient"];
                return (o == DBNull.Value) ? false : (bool)o;
            }
            set
            {
                dataRow["Inpatient"] = value;
            }
        }
        /// <summary>
        /// Boolean which indicates whether this is an IN-Facility procedure or not
        /// </summary>
        public bool IsFacility
        {
            get
            {
                object o = dataRow["IsFacility"];
                return (o == DBNull.Value || (int)o == 0) ? false : true;
            }
            set
            {
                dataRow["IsFacility"] = value;
            }
        }
        /// <summary>
        /// Boolean which indicates whether this procedure uses hospital pricing or not
        /// </summary>
        public bool UseHospitalPricing
        {
            get
            {
                object o = dataRow["UseHospitalPricing"];
                return (o == DBNull.Value || (int)o == 0) ? false : true;
            }
            set
            {
                dataRow["UseHospitalPricing"] = value;
            }
        }

        /// <summary>
        /// Professional fee associated with this procedure
        /// </summary>
        public string ProfessionalFee
        {
            get
            {
                return dataRow["ProfessionalFee"].ToString();
            }
            set
            {
                dataRow["ProfessionalFee"] = value;
            }
        }

        /// <summary>
        /// Gets the BudgetMilestoneVisits associated with this procedure
        /// </summary>
        public BudgetMilestoneVisits BudgetMilestoneVisits
        {
            get
            {
                if (budgetMilestoneVisits == null)
                {
                    budgetMilestoneVisits = new BudgetMilestoneVisits(CachePolicy);
                    budgetMilestoneVisits.Load(Utility.DataAccess.GetConnectionString(), "BudgetProcedure", Id);
                }
                return budgetMilestoneVisits;
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Initialize the base class with the specified cache policy and sets the dataRow member.
        /// </summary>
        public BudgetProcedure(ICachePolicy cachePolicy, DataRow row)
            : base(cachePolicy)
        {
            dataRow = row;
        }
        #endregion

        #region Base Methods
        /// <summary>
        /// Cache this instance using the prevalent cache policy.
        /// </summary>
        public override void Cache()
        {
            CachePolicy.Cache(BudgetProcedureKey, this);
        }

        /// <summary>
        /// Remove this instance from the cache using the prevalent cache policy.
        /// </summary>
        public override void UnCache()
        {
            CachePolicy.UnCache(BudgetProcedureKey);
        }

        /// <summary>
        /// Load method is not used
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="args"></param>
        public override void Load(string connectionString, params object[] args)
        {
            throw new Exception("BudgetProcedure is not directly loadable");
        }
        #endregion

    }// END CLASS DEFINITION BudgetProcedure
}